package cn.edu.lzu.service.impl;

import cn.edu.lzu.common.assist.DataPage;
import cn.edu.lzu.common.assist.ResponseResult;
import cn.edu.lzu.dao.*;
import cn.edu.lzu.entity.*;
import cn.edu.lzu.service.ExamineItemService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @PackageName: cn.edu.lzu.service.impl
 * @ClassName: ExamineItemServiceImpl
 * @Description: TODO
 * @Author: 张琦[QQ:3098086691]
 * @Date: 2019/12/8 20:01
 * @Version: 1.0
 */
@Service
@Transactional
public class ExamineItemServiceImpl implements ExamineItemService {
    @Resource
    private ExamineItemMapper examineItemMapper;
    @Resource
    private ExamineItemTypeMapper examineItemTypeMapper;
    @Resource
    private LabExamineClauseMapper labExamineClauseMapper;
    @Resource
    private FillLabItemMapper fillLabItemMapper;
    @Resource
    private ExaminerMapper examinerMapper;

    /**
     * @Author: 张琦[QQ:3098086691]
     * @Description: TODO 获取一页‘体检项目’数据
     * @Date: 2019/12/9 22:01
     * @Param: [unitId]
     * @Return: cn.edu.lzu.common.assist.DataPage
     **/
    @Override
    public DataPage getExamineItemPageByUnitId(long unitId, int currentPageNum, int pageSize) {
        int startIndex = (currentPageNum - 1) * pageSize;
        List<ExamineItem> examineItemList = examineItemMapper.selPageExamineItemNotDeletedByUnitId(unitId, startIndex, pageSize);
        int totalCount = examineItemMapper.selItemTotalCountByUnitId(unitId);
        DataPage itemPage = new DataPage();
        //如果没有异常
        if (totalCount >= 0 && examineItemList.size() >= 0) {
            itemPage.setMsg("成功");
            itemPage.setCode(0);
            itemPage.setData(examineItemList);
            itemPage.setTotalCount(totalCount);
        } else {
            itemPage.setMsg("失败");
            itemPage.setCode(1);
            itemPage.setData(null);
            itemPage.setTotalCount(0);
        }
        return itemPage;
    }

    /**
     * @Author: 张琦[QQ:3098086691]
     * @Description: TODO 删除一条‘体检项目’记录
     * @Date: 2019/12/9 22:01
     * @Param: [examineItemId]
     * @Return: cn.edu.lzu.common.assist.ResponseResult
     **/
    @Override
    public ResponseResult deleteExamineItemById(int examineItemId) {
        ResponseResult responseResult = new ResponseResult();
        int result = examineItemMapper.updDeleteExamineItemByItemId(examineItemId);
        if (result == 1) {
            responseResult.setCode(1);
            responseResult.setMsg("删除成功");
        } else {
            responseResult.setCode(0);
            responseResult.setMsg("删除失败");
        }
        return responseResult;
    }

    /**
     * @Author: 张琦[QQ:3098086691]
     * @Description: TODO 批量删除‘体检项目’
     * @Date: 2019/12/9 22:01
     * @Param: [examineItemIds]
     * @Return: cn.edu.lzu.common.assist.ResponseResult
     **/
    @Override
    public ResponseResult batchDeleteExamineItemsByItemIds(String examineItemIds) {
        examineItemIds = examineItemIds.substring(0, examineItemIds.length() - 1);
        int result = examineItemMapper.updBatchSetIsDeletedTrue(examineItemIds);
        ResponseResult responseResult = new ResponseResult();
        if (result == examineItemIds.split(",").length) {
            responseResult.setCode(1);
            responseResult.setMsg("项目批量删除成功");
        } else {
            responseResult.setCode(0);
            responseResult.setMsg("项目批量删除失败");
        }
        return responseResult;
    }

    @Override
    public ResponseResult executeAddExamineItem(ExamineItem examineItem) {
        int result = examineItemMapper.insExamineItem(examineItem);
        ResponseResult responseResult = new ResponseResult();
        if (result == 1) {
            responseResult.setCode(1);
            responseResult.setMsg("项目添加成功！");
        } else {
            responseResult.setCode(0);
            responseResult.setMsg("项目添加失败！");
        }
        return responseResult;
    }

    /**
     * @Author: 张琦[QQ:3098086691]
     * @Description: TODO 使用‘编号’和‘名称’实现模糊查询
     * @Date: 2019/12/10 11:39
     * @Param: [searchInput, unitId, currentPageNum, pageSize]
     * @Return: cn.edu.lzu.common.assist.DataPage
     **/
    @Override
    public DataPage fuzzySearchItemByIdAndName(String searchInput, long unitId, int currentPageNum, int pageSize) {
        if (searchInput.trim() == "") {
            return getExamineItemPageByUnitId(unitId, currentPageNum, pageSize);
        } else {
            DataPage itemPage = new DataPage();
            int startIndex = (currentPageNum - 1) * pageSize;
            List<ExamineItem> examineItemList = null;
            try {
                int examineItemId = Integer.parseInt(searchInput.trim());
                examineItemList = examineItemMapper.selFuzzyPageExamineItemById(unitId, examineItemId, startIndex, pageSize);
            } catch (NumberFormatException e) {
                String itemName = searchInput.trim();
                examineItemList = examineItemMapper.selFuzzyPageExamineItemByName(unitId, itemName, startIndex, pageSize);
            }
            int totalCount = examineItemMapper.selItemTotalCountByUnitId(unitId);
            itemPage.setData(examineItemList);
            itemPage.setTotalCount(totalCount);
            itemPage.setCode(0);
            itemPage.setMsg("搜索成功");
            return itemPage;
        }
    }

    /**
     * @Author: 张琦[QQ:3098086691]
     * @Description: TODO 根据项目类型编号itemTypeId获取本单位（unitId）中所有属于该项目类型的项目
     * @Date: 2020/1/15 13:36
     * @Param: [itemTypeId, unitId]
     * @Return: java.util.List<cn.edu.lzu.entity.ExamineItem>
     **/
    @Override
    public List<ExamineItem> getExamineItemsByTypeId(int itemTypeId, long unitId) {
        List<ExamineItem> examineItemList = examineItemMapper.selExamineItemsByTypeId(unitId);
        return examineItemList;
    }

    /**
     * @Author: 张琦[QQ:3098086691]
     * @Description: TODO 根据examineItemId获取examineItem
     * @Date: 2020/3/17 16:49
     * @Param: [examinerItemId, unitId]
     * @Return: cn.edu.lzu.entity.ExamineItem
     **/
    @Override
    public ExamineItem getExamineItemById(int examineItemId) {
        ExamineItem examineItem = examineItemMapper.selByExamineItemId(examineItemId);
        return examineItem;
    }

    /**
     * @Author: 张琦[QQ:3098086691]
     * @Description: TODO 获取实验室检查项目
     * @Date: 2020/3/21 20:40
     * @Param: [unitId]
     * @Return: java.util.List<cn.edu.lzu.entity.ExamineItem>
     **/
    @Override
    public List<ExamineItem> getLabExamineItem(long unitId) {
        List<ExamineItem> examineItemList=examineItemMapper.selExamineItemsByTypeId(unitId);
        return examineItemList;
    }

    @Override
    public Map<String, Object> getLabExaminerAndTime(int fillItemId) {
        FillLabItem fillLabItem=fillLabItemMapper.selByFillLabItemId(fillItemId);
        Examiner examiner=examinerMapper.selExaminerById(fillLabItem.getExaminerId());
        Map<String, Object> map=new HashMap<>();
        map.put("examiner", examiner);
        map.put("fillLabItem", fillLabItem);
        return map;
    }

}
